{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "%load_ext sql\n",
    "%sql sqlite://"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "%%sql drop table if exists product;\n",
    "create table product(\n",
    "       pname        varchar primary key, -- name of the product\n",
    "       price        money,               -- price of the product\n",
    "       category     varchar,             -- category\n",
    "       manufacturer varchar NOT NULL     -- manufacturer\n",
    ");\n",
    "insert into product values('Gizmo', 19.99, 'Gadgets', 'GizmoWorks');\n",
    "insert into product values('PowerGizmo', 29.99, 'Gadgets', 'GizmoWorks');\n",
    "insert into product values('MultiTouch', 203.99, 'Household', 'Hitachi');\n",
    "insert into product values('SingleTouch', 149.99, 'Photography', 'Canon');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Activity 2-2:\n",
    "-------------\n",
    "\n",
    "Single table queries"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Exercise #1\n",
    "-----------\n",
    "\n",
    "Try writing a query to get an output table of all the products with \"Touch\" in the name, showing just their name and price, and sorted alphabetically by manufacturer."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Let's look at the products first:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "%sql select * from product;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write your query here:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%%sql\n",
    "SELECT pname, price FROM product\n",
    "WHERE pname LIKE '%Touch%'\n",
    "ORDER BY manufacturer;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, write a query that returns the _distinct_ names of manufacturers that make products with \"Gizmo\" in the name:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%%sql\n",
    "SELECT DISTINCT manufacturer FROM product\n",
    "WHERE pname LIKE '%Gizmo%';"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Exercise #2:\n",
    "------------\n",
    "\n",
    "_More on ORDER BY_\n",
    "\n",
    "Try some of these queries but first guess what they return."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "%sql SELECT DISTINCT category FROM product ORDER BY category;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "%sql SELECT category FROM product ORDER BY pname;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "%sql SELECT DISTINCT category FROM product ORDER BY pname;"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
